package com.dong.vdbtc.validation;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 * description
 *
 * @author Administrator 2021/09/07 19:40
 */
public class UserValidator implements Validator {

    @Override
    public boolean supports(Class<?> aClass) {
        return User.class.equals(aClass);
    }

    @Override
    public void validate(Object o, Errors errors) {
        ValidationUtils.rejectIfEmpty(errors,"name","name is empty");
        User user = (User) o;
        if (user.getAge() < 0){
            errors.rejectValue("age","age should not be defined to 0");
        }
        if (user.getAge() >1000){
            errors.rejectValue("age","age is too long");
        }
    }
}
